package edu.cs346.group5.logic;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;

import edu.cs346.group5.backend.Database;
import edu.cs346.group5.datastores.Topic;

public class AddViewTopic {
	
	public static void AddTopic(Topic topic){
		try {
			java.util.Date today = new java.util.Date();
			java.sql.Date sqlToday = new java.sql.Date(today.getTime());
			
			Database.execRaw("insert into Items (image, reference_url, description) values (" + "'" + topic.getImageUrl() + "', " + "'" + topic.getReferenceURL() + "'," + "'" + topic.getDescription() + "'" +")");
			List<HashMap<String, Object>> results = Database.select("select * from Items where image = '" + topic.getImageUrl() + "' AND reference_url = '" + topic.getReferenceURL()+ "' AND description = '" + topic.getDescription() + "'ORDER BY id desc;");
			Database.execRaw("insert into Posts (posted_date, content, item_id, posted_by, category) values (" + "'" + sqlToday + "', " + "'" + topic.getTopicName() + "'," + "'" + results.get(0).get("id") + "'," + "'" + Login.LoggedInUser.Id + "'," + "'" + topic.getCategoryId() + "'" +")");
			
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	
	public static List<HashMap<String, Object>> GetAllCategories(){
		try {
			return Database.select("select * from Categories where archived=0");
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public static void AddCategory(String cat) {
		try {
			Database.insert("Categories", "category_name", "\""+cat+"\"");
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	
	public static void ArchiveCategory(String cat) {
		try {
			Database.execRaw("update Categories set archived=1 where category_name=\""+cat+"\"");
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

}
